package com.akami.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import xframe.ctl.XDispatchAction;
import xframe.util.db.Row;
import xframe.util.db.Transaction;

public class CommentAction extends XDispatchAction {
	public ActionForward move( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception { 

		Row	req			=	getRequestInfo(request);
		
		
		
		
		return mapping.findForward((String) req.get("NEXT_PAGE") );
	}
	
	// 리스트
	public ActionForward selectComment( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {

		Row	req			=	getRequestInfo(request);
			

		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			
	        // 전체 게시물 갯수
			String totalQueryName	=	"comment.selectCommentTotal";		
			String	totalQuery 	=	mappingQuery(request, totalQueryName);
			
			simpleExecuteQueryGetOneRow(request, totalQuery, totalQueryName);
			
			Row	SUM_LIST	=	(Row)request.getAttribute(totalQueryName);			
			req.put("TOTAL", SUM_LIST.getString("TOTAL"));					
			int start_row 	=	0;
			int page		=	0;
			//페이지 파라미터 점검
			//첫페이지는 파라미터가 없으므로 0이나 1로 셋팅한다.
			if(req.get("page") ==null || ((String)req.get("page")).trim().length() == 0 ){
				page	=	1;
			}else  {
				try{
					page	=	Integer.parseInt(((String)req.get("page")));
				}catch(Exception e){
					page	=	1;
				}
			}
			start_row	=	(page	-	1)	*	10;
			req.put("start_row", start_row+"");
			req.put("page", page+"");
			
			//전체게시물의 갯수를 토대로 다시 쿼리를 날려서 원하는 데이터만 받아온다.		
			String 	listQueryName	=	"comment.selectComment";
			String	listQuery 		=	mappingQuery(req, listQueryName);	
	
			simpleExecuteQuery(request, listQuery, listQueryName);												
	
			//디비연결 종
	        Transaction.end(request);
        
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.toString());
			return mapping.findForward( "system_error" );
		}        
		
        return mapping.findForward( "selectComment" );       
	}
	
	
	
	
	public ActionForward getSelect( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
			Row	req			=	getRequestInfo(request);
		try{
			// 디비연결 시작
			Transaction.start(request);		
			
			String 	listQueryName	=	req.getString("get");
			String	listQuery 		=	mappingQuery(req, listQueryName);	
			simpleExecuteQuery(request, listQuery, "selectxml");												
	
			//디비연결 종
	        Transaction.end(request);
        
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		}    		
	return mapping.findForward("selectxml");
	}
	
	
	public ActionForward savecomment( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		Row	req			=	getRequestInfo(request);
		try{		
		// 디비연결 시작
		Transaction.start(request);		
		
		HttpSession session = request.getSession();
		req.put("reg_email", session.getAttribute("s_email"));
		
		
		
		String queryName	=	"comment.savecomment";		
		String	query 	=	mappingQuery(req, queryName);
		String[] preparedIndex = {
				"content_html",            
				"reg_name",            
				"reg_email"            
				};
		//PreparedStatement  실행
		preparedExecuteUpdate(request, req, preparedIndex, query);		
		
		
		//전체게시물의 갯수를 토대로 다시 쿼리를 날려서 원하는 데이터만 받아온다.		
		String 	listQueryName	=	"comment.selectComment";
		String	listQuery 		=	mappingQuery(req, listQueryName);	

		simpleExecuteQuery(request, listQuery, listQueryName);	

		//디비연결 종
        Transaction.end(request);
												
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "system_error" );			
		} 
		
		return mapping.findForward("savecomment");
	}
	
	public ActionForward delComment( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		Row	req			=	getRequestInfo(request);
		try{		
			// 디비연결 시작
			Transaction.start(request);		
			
			
			String queryName	=	"comment.delComment";		
			String	query 	=	mappingQuery(req, queryName);
			String[] preparedIndex = {
					"comment_idx"            
			};
			//PreparedStatement  실행
			preparedExecuteUpdate(request, req, preparedIndex, query);		

			Transaction.end(request);
			req.put("SUCCESS_FAIL", "SUCCESS");
			req.put("MESSAGE", "삭제되었습니다.");
			
			return mapping.findForward( "success_fail" ); 
			
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "success_fail" );			
		} 
		
	}
	public ActionForward modComment( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		Row	req			=	getRequestInfo(request);
		try{		
			// 디비연결 시작
			Transaction.start(request);		
			
			
			String queryName	=	"comment.modComment";		
			String	query 	=	mappingQuery(req, queryName);
			String[] preparedIndex = {
					"content_html",
					"comment_idx"            
			};
			//PreparedStatement  실행
			preparedExecuteUpdate(request, req, preparedIndex, query);		
			
			Transaction.end(request);
			req.put("SUCCESS_FAIL", "SUCCESS");
			req.put("MESSAGE", "수정되었습니다.");
			
			return mapping.findForward( "success_fail" ); 
			
		}catch(Exception e){
			e.printStackTrace();
			Transaction.rollback(request, response);
			req.put("system_error", e.getMessage());
			return mapping.findForward( "success_fail" );			
		} 
		
	}
	
	public ActionForward codeUpdate( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		return mapping.findForward("selectCode");
	}
	
	public ActionForward codeDelete( 	ActionMapping       mapping,
			ActionForm          form,
			HttpServletRequest  request,
			HttpServletResponse response ) throws Exception {
		return mapping.findForward("selectCode");
	}
}
